<?php

class EducationExperiencesController extends AppController {

    var $helpers = array('Html', 'Form', 'Javascript', 'TinyMce');

    var $name = 'EducationExperiences';

    var $uses = array('EducationExperience', 'Title', 'Person', 'Region', 'Town', 'District'); // specifico i modelli che utilizza il controller

    function isAuthorized() {
        return true;
    }
    
    function index($person_id='', $from = '') {
        // se richiamato da countbox ritorno solo il numero di record
         if (isset($this->params['requested'])) {
            return $this->EducationExperience->find('count');
         }
         else {
            $params = array(
                        'conditions' => array('EducationExperience.person_id' => $person_id)
                    ); 
            $this->set('educationExperiences', $this->EducationExperience->find('all', $params)); // view variable
            $this->set('person', $this->EducationExperience->Person->findById($person_id));
            $this->set('from', $from);
         }
    }

    function add($person_id='0') {

        if(!empty($this->data) && $this->EducationExperience->validates()){

            if($this->EducationExperience->save($this->data)) {
                $this->Session->setFlash('L\'esperienza di istruzione/formazione &egrave; stata salvata');
                $this->redirect(array('action' => 'index', $this->data['EducationExperience']['person_id']));
            }
        }
        // preparo array delle opzioni per le select del form
        $this->set('organizations', $this->EducationExperience->Organization->find('list'));
        $this->set('titles', $this->EducationExperience->Title->find('list'));
        $this->set('positions', $this->EducationExperience->Position->find('list'));
        $this->set('regions', $this->Region->find('list'));
        $this->set('person_id', $person_id);

    }

    function edit($id) {
        // if $_POST is empty read the information from DB
         if(empty($this->data)) {
             $this->EducationExperience->id = $id;
             $this->data = $this->EducationExperience->read();
             $this->set('organizations', $this->EducationExperience->Organization->find('list'));
             $this->set('titles', $this->EducationExperience->Title->find('list'));
             $this->set('positions', $this->EducationExperience->Position->find('list'));
         }
         // if $_POST != empty save the information
         else {
             if($this->EducationExperience->save($this->data)) {
                 $this->Session->setFlash("L'esperienza di istruzione/lavoro è stata aggiornata");
                 $this->redirect(array('action' => 'index', $this->data['EducationExperience']['person_id']));
             }
         }
    }

    function delete($id, $person_id) {
        $this->EducationExperience->delete($id);
        $this->Session->setFlash('L\'esperienza è stata eliminata');
        $this->redirect(array('action' => 'index', $person_id));
    }
    
    /*
     * Add a new title to the titles table. AJAX call.
     * 
     * @return string New title id
     */
    function add_title() {
        
        $this->autoRender = false;  // non renderizza la view
        
        if($this->RequestHandler->isAjax()) {
            $this->EducationExperience->Title->save($this->params['form']);
            echo $this->EducationExperience->Title->id;
        }
        
    }
    
    /*
     * Add a new organization to the organizations table. AJAX call.
     * 
     * @return string New organization id
     */
    function add_organization() {
        
        $this->autoRender = false;  // non renderizza la view
        
        if($this->RequestHandler->isAjax()) {
            $this->EducationExperience->Organization->save($this->params['form']);
            echo $this->EducationExperience->Organization->id;
        }
        
    }
    
    /*
     * Add a new position to the positions table. AJAX call.
     * 
     * @return string New position id
     */
    function add_position() {
        
        $this->autoRender = false;
        
        if($this->RequestHandler->isAjax()) {
            $this->EducationExperience->Position->save($this->params['form']);
            echo $this->EducationExperience->Position->id;
        }
        
    }


}

?>
